<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body>
    <div id="app" class="demo">
        <div>{{name}}</div>
        <hr>
        <div>{{name2}}</div>
        <hr>
        <div>{{name3}}</div>

        <button @click="my_click">点击</button>
    </div>
</body>
<script>

    const HelloVueApp = {
        name: 'app',
        setup(props, context) {
            // 1. 传入一个函数
            const firstName = Vue.ref('hahahaha'), 
                    lastName = Vue.ref('heiheihei'),
                    allName = Vue.ref({name:'xiao mi',age:11});

            const name = Vue.computed(() => {
                return firstName.value + lastName.value
            })
            // 2. 传入一个配置对象
            let name2 = Vue.computed({
                get() {
                    return firstName.value + lastName.value
                },
                set(newValue) {
                    const names = newValue.split(' ')
                    firstName.value = names[0]
                    lastName.value = names[1]
                    console.log('字符串赋值成功')
                }
            });
            name2.value = 'hahaha hehehe';
            let name3 = Vue.computed({
                get(){
                    return allName.value;
                },
                set(newName){
                    allName.name = newName;
                    console.log('对象赋值成功')
                }
            });


            return {
                name,
                name2,
                name3
            }
        },
        methods:{
            my_click(){
                console.log("修改name2的值");
                //不能修改
                name2 = "ni hao";
                console.log("name2(原来的值没有改变，新增不影响原来的值):"+name2);

                //可以使用获取原值的方式修改(修改不了)
                let nameObj = Vue.toRaw(name3);
                nameObj = "nin hao";
                console.log("nameObj:"+nameObj);
                
            }
        }
    }
    Vue.createApp(HelloVueApp).mount('#app')
</script>

</html>